# encoding: utf-8

# 获取自己预测准确率及积分情况(微服务接口)

module Capevaluation
  module Business
    module Prediction

      #curl http://127.0.0.1:5678/api -d cmd=predict_get_scores -d type=0 -d player="prediction.FBA2017006" -d gzip=0
      #curl http://127.0.0.1:5678/api -d cmd=predict_get_scores -d type=1 -d player="13677845643" -d gzip=0
      def predict_get_scores(params)
        #self.logger.debug(CaiQiu::Support::Utils.get_backtrace_list)
        self.logger.debug(__method__.to_s + " params:#{params}")

        #1.检验参数是否为空
        if params['type'].nil? || params['player'].nil?
          raise Capevaluation::Exception::LackOfParams.new
        end

        #2.验证参数有效性
        #验证type字段
        type = params['type'].to_i
        raise Capevaluation::Exception::InvalidParams.new if !type.zero? && type!=1

        #3. 从redis中获取结果
        score_results = self.redis.get("capevaluation.score.#{params['player']}")
        if !score_results.nil?
          return {:code => 0, :msg => '', :resp => [score_results]}
          #return JSON.parse(ranking_results)
        end

        #4.查询结果
        player_scores = Capevaluation::Service::PlayerScore.get_record_by_player(params)
        ret = []
        player_scores.each do|player_score|
          ret_in = {
              "player" => player_score[:player],
              "total"=> player_score[:total],
              "success"=> player_score[:success],
              "fail" => player_score[:fail],
              "ratio"=> player_score[:ratio],
              "score"=> player_score[:score],
              "ranking"=> player_score[:ranking]
          }
          ret.push ret_in
        end
        result = {:code => 0, :msg => '', :resp => ret}
        self.redis.setex("capevaluation.score.#{params['player']}", 30, ret)
        return result

      end

    end
  end
end



